package com.zh.zhbootauth.config;

import com.zh.zhbootcommon.common.result.impl.ResultCode;
import com.zh.zhbootcommon.domain.DResult;
import com.zh.zhbootcommon.utils.req.RespUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 描述: 自定义登录失败处理类
 *
 * @author: yanglin
 * @Date: 2021-10-29-21:08
 * @Version: 1.0
 */
@Slf4j
@Component
public class LoginFailureHandlerConfig implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        log.info("登录失败, {}", request.getRequestURI());
        DResult result = DResult.of(null, ResultCode.USER_LOGIN_ERROR.getCode(), ResultCode.USER_LOGIN_ERROR.getMsg());
        RespUtil.Retuen(response, result);
    }


}
